import axios,{InternalAxiosRequestConfig,AxiosResponse,AxiosError} from 'axios'
import {message} from 'antd'
import * as constant from '@/utils/constant'

//进行基本axios的配置
axios.defaults.baseURL="http://www.zhaijizhe.cn:3005"
axios.defaults.timeout=5000
//请求拦截器进行配置:config的类型InternalAxiosRequestConfig
axios.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    const token=localStorage.getItem('token')
    if(token){
        config.headers.Authorization=token
    }
    return config
})
//响应拦截器
axios.interceptors.response.use((response:AxiosResponse)=>{
    return response
},(error:AxiosError)=>{
    if(error.response){
       switch(error.response.status){
        case 404:
            message.warning(constant.REQUESTURLERROR)
            break
        case 401:
            message.warning(constant.TOKENVALDATE)
            //删除token
            localStorage.removeItem('token')
            break;
       }
    }
    return Promise.reject(error)
})
export default axios

